package com.hp.attendance.dao;

import com.hp.attendance.entity.Menu;
import com.hp.attendance.util.BaseDAO;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class MenuDao extends BaseDAO {

    public List<Menu> getList() throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql ="select * from menu";
        List<Menu> list = queryList(sql, new Object[0], new Menu());
        return list;
    }

    /**
     * 根据员工id查询所属菜单列表
     * @param empId
     * @param pid
     * @return
     */
    public List<Menu> getMenuByEmpId(int empId,int pid) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql ="select * from menu m " +
                "    left join role_menu rm on m.id =rm.m_id " +
                "    left join role r on r.id=rm.r_id " +
                "    left join emp_role er on er.r_id=r.id " +
                "    left join employee e on er.emp_id=e.id " +
                "where e.id=? and m.pid=?";
        Object[] obj ={empId,pid};
        List<Menu> menus = queryList(sql, obj, new Menu());
        return menus;
    }

    /**
     * 分页查询记录
     * @param pageNo 当前页
     * @param pageSize 每页显示的条数
     * @return
     */
    public List<Menu> getLimitList(int pageNo,int pageSize) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql ="select * from menu limit ?,?";
        Object[] obj ={(pageNo-1)*pageSize,pageSize};
        List<Menu> menus = queryList(sql, obj, new Menu());
        return menus;
    }

    /**
     * 查询总记录数
     * @return
     * @throws SQLException
     */
    public long getTotal() throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql ="select count(1) from menu";
        int count = queryCount(sql, new Object[0]);
        return count;
    }

    /**
     * 添加菜单
     * @param menu
     */
    public void addMenu(Menu menu) throws SQLException, IOException, ClassNotFoundException {
        String sql ="insert into menu values(null,?,?,?,?)";
        Object[] obj={menu.getName(),menu.getPriority(),menu.getPath(),menu.getPid()};
        addOrUpdOrDel(sql,obj);
    }

    /**
     * 根据id查询menu对象
     * @param id  menu的主键id值
     * @return
     */
    public Menu getMenu(int id) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql ="select * from menu where id=?";
        Object[] obj = {id};
        Menu menu = queryOne(sql, obj, new Menu());
        return menu;
    }

    /**
     * 修改菜单
     * @param menu
     */
    public void updateMenu(Menu menu) throws SQLException, IOException, ClassNotFoundException {
        String sql ="update menu set name=?,priority=?,path=?,pid=? where id=?";
        Object[] obj={menu.getName(),menu.getPriority(),menu.getPath(),menu.getPid(),menu.getId()};
        addOrUpdOrDel(sql,obj);
    }

    /**
     * 删除菜单
     * @param id
     */
    public void delMenu(int id) throws SQLException, IOException, ClassNotFoundException {
        String sql ="delete from menu where id=?";
        Object[] obj ={id};
        addOrUpdOrDel(sql,obj);
    }

    /**
     * 根据角色id查询对用的菜单列表
     * @param roleId
     * @return
     */
    public List<Menu> getMenusByRoleId(int roleId) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql="select * from menu m " +
                " left join role_menu rm on rm.m_id = m.id " +
                " left join role r on rm.r_id =r.id " +
                " where r.id=?";
        Object[] obj ={roleId};
        List<Menu> list = queryList(sql, obj, new Menu());
        return list;
    }
}
